#include "GlobalHead.h"
#include "FreeCamera.h"
#include "CustomPlayer.h"
#include "App/Document.h"
using namespace App;
using namespace Part;
PROPERTY_SOURCE(Part::PartFreeCamera,Part::Primitive)
PartFreeCamera::PartFreeCamera(void)
{
	ADD_PROPERTY_TYPE(angle,(400.0f),"freeCamera",Prop_None,"FreeCamera walk speed");
	ADD_PROPERTY_TYPE(speed,(10.f),"freeCamera",Prop_None,"FreeCamera walk speed");
	ADD_PROPERTY_TYPE(fov,(54.0),"freeCamera",Prop_None,"FreeCamera field of view");
	ADD_PROPERTY_TYPE(nearClip,(54.0),"freeCamera",Prop_None,"FreeCamera  near clip");
	ADD_PROPERTY_TYPE(farClip,(4000.0),"freeCamera",Prop_None,"FreeCamera far clip");

}

PartFreeCamera::~PartFreeCamera(void)
{
	CCustomPlayer::Instance().DeleteObjectFromScene(this->Shape.getValue()/*->GetID()*/);
}

App::DocumentObjectExecReturn * PartFreeCamera::execute(void)
{
	if(isImporting())
	{
		std::string pName = getNameInDocument();
		CK3dEntity* pCamParent = static_cast<CK3dEntity*>(CCustomPlayer::Instance().createSceneObject(CKCID_3DENTITY,(char*)pName.c_str()));
		pName += "_cam";
		CKCamera* pCam = static_cast<CKCamera*>(GetCKContext(0)->CreateObject(CKCID_CAMERA,(CKSTRING)pName.c_str()));
		pCam->SetParent(pCamParent);
		this->Shape.setValue(pCamParent);
	}
	return App::DocumentObject::StdReturn;
}

short PartFreeCamera::mustExecute()const
{
	return Part::Primitive::mustExecute();
}
void  PartFreeCamera::rotate(float X,float Y,float Z,float Angle,App::DocumentObject *Ref ,CKBOOL KeepChildren)
{
	if(Ref == NULL)
		Shape.getValue()->Rotate(X,Y,Z,Angle/angle.getValue(),NULL,KeepChildren);
	else
		Shape.getValue()->Rotate(X,Y,Z,Angle/angle.getValue(),Shape.getValue(),KeepChildren);
}
void PartFreeCamera::translateObject(float X,float Y,float Z,DocumentObject* Ref,CKBOOL KeepChildren)
{
	float s = speed.getValue();
	VxVector speedvec(X,Y,Z);
	speedvec *=s;
	if(Ref)
		Shape.getValue()->Translate(&speedvec,Shape.getValue(),KeepChildren);
	else
		Shape.getValue()->Translate(&speedvec,NULL,KeepChildren);
}

